﻿using BaseDll;
using System;
using System.Net;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using System.IO;

using System.Windows.Forms;
using System.Net.Sockets;
namespace OtherDevice
{
    [Description("阿特拉斯")]
    public class Screw_Atlas:Screw_Base
    {
        public Screw_Atlas(string strName) : base(strName)
        {
            controlFrom = new Form_DP螺丝机();
        }
        Socket sockClient = null;
        Thread threadClient = null;
        bool bSendFile = false;
        private string connectstring = "00200001006        00";
        private string subscribestring = "006000080010        1201001310000000000000000000000000000001\r\n";
        private string backSubscribestring = "002900090010        120100100";
        int filesize = 6220854;
        int fileSizeCnt = 0;
        byte[] fileArray = new byte[1920 * 1080 * 4];
        public override int linkEtherNetControl()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress ip = IPAddress.Parse(paramter.str_IP地址);
            IPEndPoint endPoint = new IPEndPoint(ip, (paramter.n_Port));
            sockClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                IAsyncResult connRes = sockClient.BeginConnect(endPoint, null, null);
                connRes.AsyncWaitHandle.WaitOne(1000, true);
                if (!connRes.IsCompleted)
                {
                    sockClient.Close();
                    return -1;
                }

            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
                return -2;
            }
            threadClient = new Thread(RecMsg);
            threadClient.IsBackground = true;
            threadClient.Start();
            return 0;

        }
        /// <summary>
        /// 接受数据，并写入缓存区
        /// </summary>
        void RecMsg()
        {
            while (true)
            {
                // 定义一个2M的缓存区；
                byte[] arrMsgRec = new byte[1920 * 1080 * 4];
                int length = -1;
                try
                {
                    length = sockClient.Receive(arrMsgRec); // 接收数据，并返回数据的长度；
                    if (length < 1)
                        continue;

                    string strMsg = System.Text.Encoding.Default.GetString(arrMsgRec, 0, length);// 将接受到的字节数据转化成字符串；                                                                                                   
                                                                                                 //byte[] arr = System.Text.Encoding.Default.GetBytes(strMsg);
                    InMessageQueue(strMsg);

                }
                catch (Exception e)
                {
                    return;
                }
            }


        }
        /// <summary>
        /// 发送
        /// </summary>
        private bool Send(string strCMD)
        {
            if (sockClient == null || !sockClient.Connected)
                return false;
            byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strCMD);
            sockClient.Send(arrMsg);
            return true;
        }
        /// <summary>
        /// 扭力值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private double GetValue_Torsion(string value)
        {
            if (value.Length < 1800) { return -999; }
            int Star = value.IndexOf("QMC");
            string data2 = value.Substring(Star + 1, value.Length - 1 - Star);
            value = data2;
            int first = value.IndexOf("30230");
            int last = value.IndexOf("30231");
            if (first == -1 || last == -1 || first >= last)
            {
                return -999;
            }
            string Torsion = value.Substring(first + 4 + 12 + 1, last - first - 1 - 4 - 12);
            Torsion = Torsion.TrimStart('0');
            double b = double.Parse(Torsion.ToUpper().Split('E')[0].ToString());//整数部分
            double c = double.Parse(Torsion.ToUpper().Split('E')[1].ToString());//指数部分   
                                                                                //按照单位来算，如果这里默认接收的单位是cN·m； 
            c = Math.Pow(10, c);
            b = b * c;
            return b;

        }
        /// <summary>
        /// 角度值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public double GetValue_Angel(string str)
        {
            if (str.Length < 1800) { return -999; }
            int Star = str.IndexOf("QMC");
            string data2 = str.Substring(Star + 1, str.Length - 1 - Star);
            str = data2;
            int first = str.IndexOf("30231");
            int last = str.IndexOf("30232");
            if (first == -1 || last == -1 || first >= last)
            {
                return -999;
            }
            string Angel = Convert.ToString(str.Substring(first + 4 + 12 + 1, last - first - 1 - 4 - 12));
            Angel = Angel.TrimStart('0');
            double b = double.Parse(Angel.ToUpper().Split('E')[0].ToString());//整数部分
            double c = double.Parse(Angel.ToUpper().Split('E')[1].ToString());//指数部分
            c = Math.Pow(10, c);
            if (c > 1000000)
            {
                c = double.Parse(Angel.ToUpper().Split('E')[2].ToString());//指数部分
                c = Math.Pow(10, c);
            }
            double e = b * c;

            return e;
        }

        public override bool Function_GetScrewTorque(out double d_Torque)
        {
            try
            {
                d_Torque = 0;
                string Result = "";
                //GetData(out Result);

                return true;

            }
            catch (Exception)
            {
                d_Torque = 0;
                return false;
            }
        }

        public override bool Function_GetScrewAngle(out double d_Angle)
        {
            try
            {
                d_Angle = 0;
                return true;
            }
            catch (Exception)
            {
                d_Angle = 0;
                return false;
            }
        }
    }
}